package com.txazo.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.txazo.domain.User;
import com.txazo.service.factory.ServiceFactory;

/**
 * 用户注册Servlet
 * 
 * @author txazo
 * @since 1.0
 * 
 */
public class RegisterServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String username = req.getParameter("username");
		String password = req.getParameter("password");

		User user = new User(username, password);
		User register = ServiceFactory.getUserService().register(user);
		if (register != null) {
			req.getSession().setAttribute("user", register);
			req.getRequestDispatcher("home.jsp").forward(req, resp);
		} else {
			resp.sendRedirect("register.html");
		}
	}

}
